//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.chris.impl;

import com.chris.entity.loan;
import com.chris.utils.DbConnection;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class loanImpl {
    private final QueryRunner runner = new QueryRunner();

    public loanImpl() {
    }

    public List<loan> getByAccount(String account) throws SQLException {
        String sql = "select * from loan where account=? order by borrow_time desc ,due_time desc ,return_time desc ";
        Connection connection = DbConnection.getConnection();
        return this.runner.query(connection, sql, new BeanListHandler<>(loan.class), account);
    }

    public void insert(String account, int journal_id, Date borrow_time, Date due_time) throws SQLException {
        String sql = "insert into loan (account,journal_id,borrow_time,due_time) values(?,?,?,?)";
        Connection connection = DbConnection.getConnection();
        this.runner.execute(connection, sql, account, journal_id, borrow_time, due_time);
    }

    public void return_journal(String account, int journal_id, Date borrow_time, Date return_time) throws SQLException {
        String sql = "update loan set return_time=?,returned=? where account=?and journal_id=? and borrow_time=?";
        Connection connection = DbConnection.getConnection();
        this.runner.execute(connection, sql, return_time, 1, account, journal_id, borrow_time);
    }
}
